﻿#region Header

// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------

#endregion

#region Usings

using System;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using ScrumTable.Common.Logging;

#endregion

namespace ScrumTable.Common.Security
{
    /// <summary>
    /// Represents a MD5 hash engine.
    /// </summary>
    public class MD5Encryption
    {

        #region Declaration
        //--------------------------------------------------------------------
        // Declaration
        //--------------------------------------------------------------------
        private static string _cpuId;
        #endregion

        #region Methods

        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates the MD5 hash.
        /// </summary>
        /// <param name="text">text for hashing</param>
        /// <returns></returns>
        static public string CreateMD5Hash(string text)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Unicode.GetBytes(text));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }

        /// <summary>
        /// Creates a MD5 hash with the local CPU hash inside
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        static public string CreateMD5HashWithLocalCpuId(string text)
        {
            if(string.IsNullOrEmpty(_cpuId))
            {
                _cpuId = GetCpuId();
            }
            return CreateMD5Hash(text+_cpuId);
        }

        /// <summary>
        /// Gets the CPU Id 
        /// </summary>
        /// <returns></returns>
        public static string GetCpuId()
        {
            string key = "Win32_Processor";
            string value = "";
            try
            {
                ManagementObjectSearcher share = new ManagementObjectSearcher("select * from " + key);
                foreach (ManagementObject stuff in share.Get())
                {
                    value = stuff.GetPropertyValue("ProcessorID").ToString();
                    if (!string.IsNullOrEmpty(value))
                    {
                        break;
                    }
                }
            }
            catch (ManagementException e)
            {
                Logger<MD5Encryption>.Info(e);
            }
            return value;
        }

        #endregion

        #region Events

        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}